<?php
// https://leetcode-cn.com/problems/middle-of-the-linked-list/

class ListNode {
    public $val = 0;
    public $next = null;
    function __construct($val)
    {
        $this->val = $val;
    }
}

class Solution {
    /**
     * @param ListNode $head
     * @return ListNode
     */
    function middleNode($head) {
        if ($head == null || $head->next == null) {
            return  $head;
        }
        if ($head->next->next == null) {
            return $head;
        }
        $slow = $head;
        $fast = $head->next;
        while ($fast->next != null && $fast->next->next != null) {
            $slow = $slow->next;
            $fast = $fast->next->next;
        }
        $slow->next = $slow->next->next;
        return $head;
    }
}